#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
import webapp2

from google.appengine.ext import db
from google.appengine.api import users
import urllib
		
class Greeting(db.Model):
  author = db.UserProperty()
  content = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp2.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    x = self.request.query_string
    if (x == 'country'):
      fp = open('country2.csv','r')
      heading = []
      data = []
      dict = {}
      for line in fp.read().split('\n'):
        if (line[:11] == 'CountryCode'):
          for x in line.split(','):
            heading.append(x)
        else:      
          try:
            tmparray = line.split(',')   
            dict[tmparray[0]] = tmparray 
          except:
            pass
      for x in dict:
        try:
          y  = dict.get(x)
          self.response.out.write(y[0] + ',' + y[1] + ';' + '\n')
        except:
          pass
      fp.close
    elif (x == 'basket'):
      fp = open('basket.csv','r')
      heading = []
      data = []
      dict = {}
      for line in fp.read().split('\n'):
        if (line[:6] == 'Basket'):
          for x in line.split(','):
            heading.append(x)
        else:      
            try:
              tmparray = line.split(',')   
              dict[tmparray[0]] = tmparray 
            except:
              pass
      for x in dict:
        try:
          y  = dict.get(x)
          self.response.out.write(y[0] + ';' + '\n')
        except:
          pass
      fp.close  
    elif (x[:12] == 'BasketStocks'):
      #self.response.out.write(x)
      #self.response.out.write(x[12:])
      fp = open('basket.csv','r')
      heading = []
      data = []
      dict = {}
      for line in fp.read().split('\n'):
        if (line[:6] == 'Basket'):
          for h in line.split(','):
            heading.append(h)
        else:      
            try:
              tmparray = line.split(',')  
              #self.response.out.write(tmparray[1] + '\n')
              #self.response.out.write( x[12:] + ' ' + tmparray[0]+ '\n')
              #self.response.out.write(tmparray[1])
              #self.response.out.write(tmparray [0] + ';' +  tmparray [1])			  
              if x[12:] == str(tmparray[0]):
                self.response.out.write(tmparray[1] + ';' + '\n')
                #dict[tmparray [1]] = tmparray [1]
            except:
              #self.response.out.write(tmparray [0] + ';' +  tmparray [1])		
              self.response.out.write(line + 'error')
              pass
      for x in dict:
        try:
          y  = dict.get(x)
          #self.response.out.write(x + '\n')
          #self.response.out.write(y[0] + '\n')
        except:
          pass
      fp.close
    else:
      self.response.out.write('Put url param')

class Guestbook(webapp2.RequestHandler):
  def post(self):
    greeting = Greeting()

    if users.get_current_user():
      greeting.author = users.get_current_user()

    greeting.content = self.request.get('content')
    greeting.put()
    self.redirect('/')


app = webapp2.WSGIApplication([
  ('/', MainPage),
  ('/sign', Guestbook)
], debug=True)
